// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 15-1-5.
 */
package gui.component.mapEditor.block
{
    import data.BlockVo;

    import flash.display.DisplayObjectContainer;
    import flash.display.Shape;
    import flash.display.Sprite;

    import gui.component.mapEditor.block.ext.BlockTileExtInfoRenderBasic;
    import gui.component.mapEditor.block.ext.MonsterBlockTileExtInfoRender;

    public class BlockTileRender
    {
        private var mContainer:Sprite;
        private var mVo:BlockVo;

        private var mBackGround:Shape;

        private var mAllExtRenderInfoVector:Vector.<BlockTileExtInfoRenderBasic>;

        public function BlockTileRender()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize(_vo:BlockVo):void
        {
            mContainer = new Sprite();
            mBackGround = new Shape();
            mContainer.addChild(mBackGround);
            mVo = _vo;

            mAllExtRenderInfoVector = new Vector.<BlockTileExtInfoRenderBasic>();
            doPushExtInfoRender(new MonsterBlockTileExtInfoRender());
        }

        public function dispose():void {}

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function refresh():void
        {
            mBackGround.graphics.clear();
            mBackGround.graphics.beginFill(MapEditorConfig.getBlockBgColorByType(mVo.type));
            mBackGround.graphics.drawRect(0,
                                          0,
                                          MapEditorConfig.EACH_BLOCK_SIZE * mVo.width,
                                          MapEditorConfig.EACH_BLOCK_SIZE * mVo.height);

            mContainer.x = MapEditorConfig.EACH_BLOCK_SIZE * mVo.x;
            mContainer.y = MapEditorConfig.EACH_BLOCK_SIZE * mVo.y;

            for each(var extInfoRender:BlockTileExtInfoRenderBasic in mAllExtRenderInfoVector)
            {
                if (extInfoRender.isCanRenderExtInfo(mVo.type))
                {
                    extInfoRender.show(mVo);
                }
                else
                {
                    extInfoRender.hide();
                }
            }

        }

        public function get vo():BlockVo {return mVo;}

        public function get container():DisplayObjectContainer {return mContainer;}

        //=================================//
        //====== Private Function =========//
        //=================================//

        private function doPushExtInfoRender(_extInfoRender:BlockTileExtInfoRenderBasic):void
        {
            _extInfoRender.initialize(mContainer);
            mAllExtRenderInfoVector.push(_extInfoRender);
        }

    }
}
